﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateUserHandler : ICommandHandler<CreateOrUpdateUserCommand>
    {
        private readonly IUserRepository userRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateUserHandler(IUserRepository userRepository, IUnitOfWork unitOfWork)
        {
            this.userRepository = userRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateUserCommand command)
        {
            var user = new User
            {
                ID = command.ID,
                Active=command.Active,
                Avartar=command.Avartar,
                CompanyID=command.CompanyID,
                CreatedDate=command.CreatedDate,
                Email=command.Email,
                FullName=command.FullName,
                GroupID=command.GroupID,
                LastLoginDate=command.LastLoginDate,
                LastPasswordChangedDate=command.LastPasswordChangedDate,
                UserName=command.UserName,
                Password=command.Password,
                Phone=command.Phone
            };
            if (user.ID == 0)
                userRepository.Add(user);
            else
                userRepository.Update(user);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateUserCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateUserCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
